# -*- coding: utf_8 -*-
# @Time : 2021/8/12 0012 12:16
# @Author : lyw
# @Remark : 门禁人员
import time

from mxsoftpy import TreeDB

from utils.public.janitor_serial import Serial


class PersonnelDB(TreeDB):
    def query_personnel(self, export=None, **kwargs):
        total, personnel_key = self.open("Janitor", "personnel", file="IOT").filter(**kwargs)
        res_list = []
        if export is not None:
            iter_obj = iter([i + 1 for i in range(len((personnel_key)))])
        else:
            ser = Serial(total, kwargs["page_size"], kwargs["page_index"])
            iter_obj = iter(ser)
        for i in personnel_key:
            value = self.open("Janitor", "personnel\\%s" % i, file="IOT").items(
                ["update_time", "badge_number", "card_number", "fingerprint", "name", "serial_num", "state", "is_del"])
            value["id"] = next(iter_obj)
            if value.get("update_time"):
                value["update_time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(value["update_time"]))
            else:
                value["update_time"] = ""
            value["state"] = "停用" if value.pop("is_del") else "在用"
            res_list.append(value)
        return total, res_list
